package fr.ul.ade.secunde.dev;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import fr.ul.ade.secunde.database.AdeWebContract;
import fr.ul.ade.secunde.util.ScheduleConstants;

/**
 * Classe contenant des méthodes auxiliaires pour le développement de
 * l'application.
 * 
 * <p>
 * Cette classe ainsi que le package devra être supprimée à la fin de la phase
 * de développement. //TODO A supprimer à la fin du développement
 * 
 * @author Michaël Schnell
 * @since Jul 7, 2013
 * 
 * @version 1.4
 */
@SuppressWarnings("all")
public class Development {

	static long[] projectIds;

	static long[] scheduleIds;

	public static void fillProjects(Context context) {
		final ContentResolver contentResolver = context.getContentResolver();

		// Suppression des anciens projets
		// final Cursor projects = contentResolver
		// .query(AdeWebContract.Project.CONTENT_URI,
		// new String[] { AdeWebContract.Project._ID },
		// null,
		// null,
		// AdeWebContract.Project.DEFAULT_SORT);
		// while (projects.moveToNext()) {
		// contentResolver.delete(AdeWebContract.Project.CONTENT_URI,
		// BaseColumns._ID + "=?",
		// new String[] { Long.toString(projects.getLong(0)) });
		// }
		contentResolver.delete(AdeWebContract.Project.CONTENT_URI, null, null);

		// Ajout des projets
		long[] id = { 1, 2, 3 };
		String[] name = { "Esial", "Projet principal", "Ex-UHP" };
		long[] project_id = { 4, 11, 6 };
		boolean[] loaded = { true, true, false };
		int nProjects = name.length;
		projectIds = new long[nProjects];
		for (int project = 0; project < nProjects; ++project) {
			ContentValues values = new ContentValues();
			values.put(AdeWebContract.Project._ID, id[project]);
			values.put(AdeWebContract.Project.PROJECT_ADEWEB_ID,
					project_id[project]);
			values.put(AdeWebContract.Project.PROJECT_NAME, name[project]);
			values.put(AdeWebContract.Project.PROJECT_LOADED, loaded[project]);

			projectIds[project] = ContentUris.parseId(contentResolver
					.insert(AdeWebContract.Project.CONTENT_URI, values));
		}
	}

	public static void fillSchedules(Context context) {
		final ContentResolver contentResolver = context.getContentResolver();

		// Suppression des anciens edts
		contentResolver.delete(AdeWebContract.Schedule.CONTENT_URI, null, null);

		// Ajout des edts
		String[] name = { "Mine" };
		String[] description = { "Edt perso de test." };
		int[] period = { 28 };
		long[] project_id = { projectIds[1] };
		String[] resources = { "12" };
		String[] studentNumbers = { "30911759" };
		int nSchedules = name.length;
		scheduleIds = new long[nSchedules];
		for (int schedule = 0; schedule < nSchedules; ++schedule) {
			ContentValues values = new ContentValues();
			values.put(AdeWebContract.Schedule.SCHEDULE_NAME, name[schedule]);
			values.put(AdeWebContract.Schedule.SCHEDULE_DESCRIPTION,
					description[schedule]);
			values.put(AdeWebContract.Schedule.SCHEDULE_PERIOD,
					period[schedule]);
			values.put(AdeWebContract.Schedule.SCHEDULE_PROJECT,
					project_id[schedule]);
			values.put(AdeWebContract.Schedule.SCHEDULE_RESOURCES,
					resources[schedule]);
			values.put(AdeWebContract.Schedule.SCHEDULE_STUDENT_NUM,
					studentNumbers[schedule]);

			values.put(AdeWebContract.Schedule.SCHEDULE_STATE,
					ScheduleConstants.DEFAULT_SCHEDULE_STATE);
			values.put(AdeWebContract.Schedule.SCHEDULE_NOTIFY_UPDATE,
					ScheduleConstants.DEFAULT_SCHEDULE_NOTIFY_UPDATE);

			scheduleIds[schedule] = ContentUris.parseId(contentResolver
					.insert(AdeWebContract.Schedule.CONTENT_URI, values));
		}
	}

	public static void fillLessons(Context context) {
		final ContentResolver contentResolver = context.getContentResolver();

		// Suppression des anciens cours
		for (long scheduleId : scheduleIds) {
			contentResolver.delete(AdeWebContract.Lesson
					.buildDisplayUri(scheduleId), null, null);
		}

		// Ajout des cours
		String[] title = { "Introduction à Android" };
		String[] date = { "2013/09/02" };
		String[] startTime = { "10:00" };
		String[] endTime = { "11:30" };
		long[] schedule = { scheduleIds[0] };
		int[] change = { 0 };
		String[] rooms = { "Amphi 01" };
		String[] groups = { "Master 2" };
		String[] teachers = { "" };
		String[] color = { "254859" };
		String[] note = { "254859" };
		String[] guid = { "1234567" };
		int[] deleted = { 0 };
		String[] last_update = { "2013/08/18 18:32" };
		int nLessons = title.length;
		for (int lesson = 0; lesson < nLessons; ++lesson) {
			ContentValues values = new ContentValues();
			values.put(AdeWebContract.Lesson.LESSON_TITLE, title[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_DATE, date[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_START_TIME,
					startTime[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_END_TIME, endTime[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_SCHEDULE, schedule[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_CHANGE, change[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_ROOMS, rooms[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_GROUPS, groups[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_TEACHERS, teachers[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_COLOR, color[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_NOTE, note[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_GUID, guid[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_DELETED, deleted[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_LAST_UPDATE,
					last_update[lesson]);
			values.put(AdeWebContract.Lesson.LESSON_LAST_UPDATE,
					last_update[lesson]);

			contentResolver.insert(AdeWebContract.Lesson
					.buildDisplayUri(schedule[lesson]), values);
		}
	}

}// class Development